#include "posiPannel.h"

bool PosiPannel::eventFilter(QObject *watched, QEvent *event){
    if(event->type() == QEvent::MouseButtonRelease){
        onSelectRangeChanged();
    }
    return QWidget::eventFilter(watched, event);
}

PosiPannel::PosiPannel(int rowNum, int colNum, int model, QWidget* parent) : _rowNum(rowNum), _colNum(colNum), _model(model){


    this->setParent(parent);
    this->installEventFilter(parent);
    this->viewport()->installEventFilter(this);

    _rowNum = std::min(_rowNum, 24);
    _colNum = std::min(_colNum, 24);
    _rowNum = std::max(_rowNum, 0);
    _colNum = std::max(_colNum, 0);

    this->setRowCount(_rowNum);
    this->setColumnCount(_colNum);
    this->setEditTriggers(QAbstractItemView::NoEditTriggers);

    this->setStyleSheet("border: 0 solid #FFFFFF;");
    this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    this->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    this->horizontalHeader()->setStyleSheet("QHeaderView::section{background:#EBF3FF; border: solid;}");
    this->horizontalHeader()->setFixedHeight(30);
    this->verticalHeader()->setStyleSheet("QHeaderView::section{background:#EBF3FF; border: solid;}");
    this->verticalHeader()->setFixedWidth(30);
    this->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
    this->setCornerWidget(new QWidget(this));
    this->cornerWidget()->setStyleSheet("background:#EBF3FF; border: solid;");
    if(_model == 0){
        this->setSelectionMode(QAbstractItemView::NoSelection);
    }else{
        this->setSelectionMode(QAbstractItemView::MultiSelection);
    }
    // 初始化表头
    QStringList vheader;
    for(int i = 0; i < _rowNum; i ++){
        vheader.push_back(QString('A'+i));
    }this->setVerticalHeaderLabels(vheader);
    // 初始化表格文字
    for(int r = 0; r < _rowNum; r ++){
        for(int c = 0; c < _colNum; c ++){
            this->setItem(r, c, new QTableWidgetItem(QString('A'+r) + QString::number(c+1)));
        }
    }

    connect(this, SIGNAL(cellClicked(int, int)), this, SLOT(onCellClicked(int, int)));


}

void PosiPannel::onSelectRangeChanged(){

    if(_model == 0)  return;

    QList<QTableWidgetSelectionRange> rangeList = this->selectedRanges();


    QVector<QString>        selectedList;
    foreach(QTableWidgetSelectionRange range, rangeList){
        for(int r = range.topRow(); r <= range.bottomRow(); r ++){
            for(int c = range.leftColumn(); c <= range.rightColumn(); c ++){
                if(this->item(r, c)->text() != ""){
                    selectedList.append(this->item(r, c)->text());
                }
            }
        }
        this->setRangeSelected(range, false);
    }

    bool hasWhite = false;
    foreach(QString posi, selectedList){
        if(! _selectList.contains(posi)){
            hasWhite = true;
            break;
        }
    }
    if(hasWhite == true){
        foreach(QString posi, selectedList){
            if(!_selectList.contains(posi)){
                _selectList.push_back(posi);
            }
        }
    }else{
        foreach(QString posi, selectedList){
            if(_selectList.contains(posi)){
                _selectList.removeAt(_selectList.indexOf(posi));
            }
        }
    }
    updateColor();
    emit sigSelectChanged(_selectList);

}

void PosiPannel::onCellClicked(int row, int col){

    if(_model == 1)  return;

    if(this->item(row,col)->text() == ""){      // 无文字时不可点
        return;
    }
    _selectList.clear();
    _selectList.push_back(this->item(row,col)->text());
    updateColor();
    emit sigSelectChanged(_selectList);
}

void PosiPannel::setSelectCell(QStringList cells){
    _selectList = cells;
    updateColor();
}

void PosiPannel::setNormCell(QStringList cells){
    _normList = cells;
    updateColor();
}

void PosiPannel::setSelectableCell(QStringList cells){
    _selectableList = cells;
    for(int r = 0; r < _rowNum; r ++){
        for(int c = 0; c < _colNum; c ++){
            QString name = QString('A'+r) + QString::number(c+1);
            if(_selectableList.contains(name)){
                this->item(r, c)->setText(name);
            }else{
                this->item(r, c)->setText("");
            }
        }
    }
}

void PosiPannel::setSelectColor(QColor color){
    _selectColor = color;
    updateColor();
}

void PosiPannel::updateColor(){
    for(int r = 0; r < _rowNum; r ++){
        for(int c = 0; c < _colNum; c ++){
            if(_selectList.contains(this->item(r,c)->text())){
                this->item(r, c)->setBackground(_selectColor);
            }else if((!_normMode) ||_normList.contains(this->item(r, c)->text())){
                this->item(r, c)->setBackground(QColor("#FFFFFF"));
            }else{
                this->item(r, c)->setBackground(QColor("#F2F2F2"));
            }
        }
    }
}


void PosiPannel::setNormMode(bool mode){

    _normMode = mode;


}


QStringList PosiPannel::getSelectList(){
    return _selectList;
}


